<script setup lang="ts">
import { requiredCheck } from "@/utils/verify";
import { ref, onMounted } from "vue";
import { addData, editData } from "@/api/modules/system/account";
import { cfgList, cfgSetSyscfg, cfgRestart, cfgReboot, cfgReset, cfgClearlog,srvcfgRestart } from "@/api/modules/system/params";
import { ElMessage, ElMessageBox } from "element-plus";

interface Props {
  title?: string;
  rowData?: Object;
  type: string;
}

const props = withDefaults(defineProps<Props>(), {
  title: "新增账号",
  rowData: undefined,
  type: "1"
});
defineOptions({
  name: "UpdateForm" // 默认策略
});
import useUpdate from "@/hooks/useUpdate";
import { ifaceAll, ifacemodeUpdate } from "@/api/modules/dynamic-camouflage/line-config";
import BaseButton from "@/components/BaseButton/index.vue";

const emits = defineEmits(["success"]);
const { updateFormData, updateFormRef, updateFormRules, submit, showHandle, openHandle, closeHandle } = useUpdate({
  rules: {
    name: [...requiredCheck("请输入用户名")],
    password: [...requiredCheck("请输入密码")],
    passwordRe: [
      {
        validator: (rule, value, callback) => {
          if (value !== updateFormData.password) {
            callback(new Error("两次输入的密码不一致"));
          } else {
            callback();
          }
        },
        trigger: "blur"
      }
    ]
  },
  defaultData: {
    loginTime: 900
  }
});
const ifaces = ref([]);

async function getIface() {
  try {
    const res = await ifaceAll();
    console.log(res);
    if (res) {
      ifaces.value = Object.values(res);
      console.log(ifaces.value);
    }
    Object.values(res);
  } catch (error) {}
}

async function submitCb() {
  await submit({
    addApi: addData,
    updateApi: editData,
    sendData: {
      name: updateFormData.name,
      passwordRe: updateFormData.passwordRe || "",
      password: updateFormData.password || "",
      role: updateFormData.role,
      id: updateFormData?.id
    }
  });
  emits("success");
}

async function getCfg() {
  const res = await cfgList();
  console.log(res);
  updateFormData.loginTime = res.result.data.timeout;
}

async function setCfg() {
  if (props.type == 1) {
    await ElMessageBox.confirm("您确定虚拟服务重启吗?", "温馨提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    });
    await srvcfgRestart();
  }
  if (props.type == 2) {
    await ElMessageBox.confirm("您确定程序重启吗?", "温馨提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    });
    await cfgRestart();
  }
  if (props.type == 3) {
    await ElMessageBox.confirm("您确定恢复出厂吗?", "温馨提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    });
    await cfgReset();
  }
  if (props.type == 4) {
    await ElMessageBox.confirm("您确定系统重启吗?", "温馨提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    });
    await cfgReboot();
  }
  if (props.type == 5) {
    await ElMessageBox.confirm("您确定清空日志吗?", "温馨提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    });
    await cfgClearlog();
  }
  ElMessage.success("更新成功");
  getCfg();
}

onMounted(() => {
  getCfg();
});
</script>

<template>
  <el-card style="width: 19%;">
    <template #header>
      <div class="card-header">
        <span>{{ title }}</span>
      </div>
    </template>
    <div @click="setCfg" style="width: 100%; display: flex; justify-content: center">
      <el-button type="primary" v-if="type == 2">{{ title }}</el-button>
      <el-button type="primary" v-if="type == 4">{{ title }}</el-button>
      <el-button type="danger" v-if="type == 1">{{ title }}</el-button>
      <el-button type="danger" v-if="type == 3">{{ title }}</el-button>
      <el-button type="success" v-if="type == 5">{{ title }}</el-button>
    </div>
  </el-card>
</template>

<style lang="scss" scoped></style>
